import request from '@/utils/request';
import {getRefreshToken} from '@/utils/auth';


let promise;

export function refreshToken(data) {
  if (promise) {
    return promise;
  }
  promise = new Promise(async (resolve) => {
    console.log('刷新token');
    const resp = await request.post('/crm/api/refresh/token', {
        refreshToken: getRefreshToken(),
        userNo: "12138"
      },
      {
        headers: {
          Authorization: `Bearer ${getRefreshToken()}`,
        },
        __isRefreshToken: true,
      });
    console.log("resp:", JSON.stringify(resp));
    resolve(resp);
  });
  promise.finally(() => {
    promise = null;
  });
  return promise;
}

export function isRefreshRequest(config) {
  if (config.__isRefreshToken === undefined) {
    return false;
  } else {
    return config.__isRefreshToken;
  }
}
